package com.lt.integration.jpa.component.hibernate.json;

import com.google.common.base.Strings;

import java.util.Properties;

public class JsonListTypeDescriptor extends JsonTypeDescriptor {
	private static final long serialVersionUID = 8429846928352375L;

	// 不能使用单列模式
//	 public static final JsonListTypeDescriptor INSTANCE = new JsonListTypeDescriptor();

	private Class<?> clazz;
	private ParameterType parameterType;
//	private String entity;
//	private String property;

	@Override
	public void setParameterValues(Properties parameters) {
		this.parameterType = (ParameterType) parameters.get(PARAMETER_TYPE);
		this.clazz = parameterType.getReturnedClass();
//		this.entity = (String) parameters.get(ENTITY);
//		this.property = (String) parameters.get(PROPERTY);
	}

	@Override
	public Object fromString(String str) {
		if (Strings.isNullOrEmpty(str)) {
			return null;
		}

		// if (List.class.isAssignableFrom(clazz)) {
		//// Type[] types = ReflectionUtil.getGenericType(entity, property);
		// if (null != types && types.length > 0){
		// @SuppressWarnings({ "unchecked", "rawtypes" })
		// List<?> list = JSON.parseArray(str, (Class)types[0]);
		// return list;
		// }
		// }
//		return JSON.parseObject(str, clazz);
		return JacksonUtil.fromString(str, clazz);
	}

}
